package com.alibaba.goods.order.service.impl;

import com.alibaba.goods.order.dao.OderDao;
import com.alibaba.goods.order.entity.Order;
import com.alibaba.goods.order.service.AccountService;
import com.alibaba.goods.order.service.OrderService;
import com.alibaba.goods.order.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static java.awt.SystemColor.info;

/**
 * @Author:RenPu
 * @Date: 2020/7/7 13:05
 * @Version: 1.0
 * @description:
 */

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {


    @Resource
    private OderDao dao;

    @Resource
    private AccountService accountService;


    @Resource
    private StorageService storageService;




    @Override
    //使用alibaba的seata来做分布式事务的处理
    @GlobalTransactional(name = "goods_order_t",rollbackFor = Exception.class)
    public void create(Order order) {

        log.info( "--------开始创建订单-------" );
        //1：新建订单
        dao.create(order);

        log.info( "------------订单微服务开始调用库存，进行扣减count----------------" );
        //2:扣减库存
        storageService.decrease(order.getProductId(),order.getCount());
        log.info( "------------订单微服务开始调用库存，进行扣减结束----------------" );

        log.info( "------------订单微服务开始调用账户，进行扣减money----------------" );
        //3:扣减账户金额
        accountService.decrease( order.getUserId(),order.getMoney() );
        log.info( "------------订单微服务开始调用账户，进行扣减end----------------" );

        //4:修改订单状态，1代表完结订单

        log.info( "------>修改订单开始<--------------" );
        dao.updateOrder( order.getUserId(),0 );
        log.info( "------>修改订单结束<--------------" );

        log.info( "------------下订单结束了o(*￣︶￣*)o-----------" );


    }

    @Override
    public void updateOrder(Long userId, Integer status) {

    }
}
